package com.twetec.cms.adapter.pc.backend;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import com.twetec.cms.model.po.base.BasePO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Profile;

import java.util.Arrays;
import java.util.List;

/**
 * MyBatisPlus代码生成器
 */
@Profile("dev")
@SpringBootTest
public class MyBatisPlusCodeGenerator {

    /**
     * 代码生成的根路径
     */
//    private static final String GEN_CODE_PATH = "E:/workspace-XMUnionEduOnline/SBProject-EduOnlineServer/EduOnlineServer/eol-foundation-mservice/foundation-mservice-edu-v1/src/main/java";
//    private static final String GEN_CODE_PATH = "E:\\workspace-XMUnionEduOnline\\SBProject-EduOnlineServer\\EduOnlineServer\\eol-foundation-mservice\\foundation-mservice-cms-v1\\src\\main\\java";
//    private static final String GEN_CODE_PATH = "D:/workproject/SBProject-EduOnlineServer-1/EduOnlineServer/eol-foundation-mservice/foundation-mservice-acl-v1/src/main/java";
//    private static final String GEN_CODE_PATH = "D:\\Project\\CMS-Project\\SBProject-CMSServer\\CMSServer\\cms-adapter-pc-backend\\src\\main\\java";

    /**
     * 数据源
     */
//    private static final String DATA_SOURCE_URL = "jdbc:mysql://47.107.81.87:23306/mdb_eol_edu?serverTimezone=GMT%2B8&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8";
//    private static final String DATA_SOURCE_URL = "jdbc:mysql://47.107.81.87:23306/mdb_eol_cms?serverTimezone=GMT%2B8&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8";
    private static final String DATA_SOURCE_URL = "jdbc:oracle:thin:@10.23.222.148:20408:helowin";

    /**
     * 数据库表前缀
     */
//    private static final String DATA_SOURCE_TABLE_PREFIX = "";

    /**
     * 数据库连接驱动
     */
    private static final String DATA_SOURCE_DRIVER_NAME = "oracle.jdbc.driver.OracleDriver";

    /**
     * 数据库连接账号
     */
    private static final String DATA_SOURCE_CONN_USERNAME = "zxfb";

    /**
     * 数据库连接密码
     */
    private static final String DATA_SOURCE_CONN_PASSWORD = "zgh_zxfb";

    /**
     * 指定要生成映射代码的表
     */
    private static final String[] GEN_CODE_TABLES = {
//            "WEBSITE_SITE",
//            "WEBSITE_COLUMN",
//            "WEBSITE_SITE_COLUMN",
//            "ARTICLE",
//            "COLUMN_ARTICLE",
//            "WEBSITE_ARTICLE_MODEL",
//            "SITE_CLUSTER_SERVER",
//            "COLUMN_ARTICLE_MODEL",
//            "ARTICLE_MODEL",
//            "WEBSITE_TEMPLATE",
//            "ARTICLE",
//            "ARTICLE_EXTEND"
//            "ARTICLE_ATTACHMENT",
//            "ARTICLE_RELEVANT",
//            "STATIC_LOG",
//            "SITE_DEPLOY_SERVER",
            "STATISTIC_ARTICLE_ADD",
            "STATISTIC_ARTICLE_PAGE_VIEWS",
            "STATISTIC_COLUMN_PAGE_VIEWS",
            "STATISTIC_SITE_PAGE_VIEWS"
    };

    @Test
    public void code() {
        // 1、创建代码生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();

        // 2、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        // 代码生成的根路径
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + "/src/main/java");
        //globalConfig.setOutputDir(GEN_CODE_PATH);
        globalConfig.setAuthor("ZCZ");
        // 生成后，是否打开资源管理器
        globalConfig.setOpen(false);
        // 生成的代码是否覆盖原来的
        globalConfig.setFileOverride(true);

        // 设置文件生成的规则
        globalConfig.setServiceName("%sService");
//         globalConfig.setControllerName("%sController");
        globalConfig.setEntityName("%sPO");

        // 统一设置实体的主键策略
        globalConfig.setIdType(IdType.ASSIGN_ID);
        // 统一设置实体日期类型
        globalConfig.setDateType(DateType.ONLY_DATE);

        // 设置全局配置到生成器中
        autoGenerator.setGlobalConfig(globalConfig);

        // 3、数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();

        dataSourceConfig.setUrl(DATA_SOURCE_URL);
        // dsc.setSchemaName("public");
        dataSourceConfig.setDbType(DbType.ORACLE);
        dataSourceConfig.setDriverName(DATA_SOURCE_DRIVER_NAME);
        dataSourceConfig.setUsername(DATA_SOURCE_CONN_USERNAME);
        dataSourceConfig.setPassword(DATA_SOURCE_CONN_PASSWORD);

        autoGenerator.setDataSource(dataSourceConfig);

        // 4、包名配置
        PackageConfig packageConfig = new PackageConfig();

        // 包名组合为parent.moduleName

        packageConfig.setParent("com.twetec.cms.adapter.pc.backend");
        packageConfig.setModuleName("statistic");

        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setMapper("mapper");
        packageConfig.setEntity("po");

        autoGenerator.setPackageInfo(packageConfig);

        // 5、配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 不生成控制器
        templateConfig.setController(null);

        autoGenerator.setTemplate(templateConfig);

        // 6、策略配置
        StrategyConfig strategyConfig = new StrategyConfig();

        // 设置表前缀，这样表名的前缀就不会映射到java类名上
//        strategyConfig.setTablePrefix(DATA_SOURCE_TABLE_PREFIX);
        // 数据库表名到java实体名的映射策略(_转驼峰)
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        // 数据库表字段名到java实体字段名的映射策略(_转驼峰)
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);

        // 逻辑删除字段映射
        strategyConfig.setLogicDeleteFieldName("is_deleted");
        // 以is_为前缀的表字段名映射到java实体字段名时，是否移除
        strategyConfig.setEntityBooleanColumnRemoveIsPrefix(true);

        // 设置版本控制字段
        strategyConfig.setVersionFieldName("version");

        // 自动填充
        TableFill gmtCreateTableFill = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModifiedTableFill = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);

        List<TableFill> tableFillList = Arrays.asList(gmtCreateTableFill, gmtModifiedTableFill);
        strategyConfig.setTableFillList(tableFillList);


        // 指定要为哪些表，自动生成代码
        strategyConfig.setInclude(GEN_CODE_TABLES);

        // 实体是否使用LombokModel
        strategyConfig.setEntityLombokModel(false);

        // 实体是否有父类
        strategyConfig.setSuperEntityClass(BasePO.class);
        strategyConfig.setSuperEntityColumns("id", "gmt_create", "gmt_modified", "creator_id", "modifier_id", "is_deleted", "version", "data_org");

        // controller配置（备用）
//        rest风格controller
//        strategyConfig.setRestControllerStyle(true);
        // 将路由的驼峰转中划线
//        strategyConfig.setControllerMappingHyphenStyle(true);

        autoGenerator.setStrategy(strategyConfig);

        // 7、执行代码生成

        autoGenerator.execute();
    }

}
